package com.smarttest.portal.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttest.common.beans.PaginationResult;
import com.smarttest.common.beans.SFTPOPBean;
import com.smarttest.common.util.FtpUtil;
import com.smarttest.portal.mapper.AutoCasesMapper;
import com.smarttest.portal.mapper.AutoDatasInfMapper;
import com.smarttest.portal.pojo.AutoCases;
import com.smarttest.portal.pojo.AutoCasesExample;
import com.smarttest.portal.pojo.AutoCasesExample.Criteria;
import com.smarttest.portal.pojo.AutoDatasInf;
import com.smarttest.portal.pojo.AutoDatasInfExample;
import com.smarttest.portal.service.Autocase_Mgmt_Service;

@Service("infAuto")
public class Autocase_Mgmt_Inf_ServiceImpl implements Autocase_Mgmt_Service {
	
	@Value("${FTP_ADDRESS}")
	private String FTP_ADDRESS;
	@Value("${FTP_PORT}")
	private Integer FTP_PORT;
	@Value("${FTP_USERNAME}")
	private String FTP_USERNAME;
	@Value("${FTP_PASSWORD}")
	private String FTP_PASSWORD;
	@Value("${FTP_BASE_PATH_AUTO}")
	private String FTP_BASE_PATH;
	@Autowired
	private AutoCasesMapper mapper;
	@Autowired
	private AutoDatasInfMapper mapperInf;

	@Override
	public int createCase(String name, String clazz, String scenes, String owner, String remark, int level, String url) {
		// TODO Auto-generated method stub
		AutoCases auto = new AutoCases();
		auto.setName(name);
		auto.setClazz(clazz);
		auto.setScens(scenes);
		auto.setOwner(owner);
		auto.setRemark(remark);
		auto.setLevel(level);
		auto.setUrl(url);
		auto.setIsdelete(0);
		auto.setCreated(new Date());
		auto.setUpdated(new Date());
		int i = mapper.insert(auto);
		return i;
	}

	@Override
	public PaginationResult searchCase(String name, String classes, String scenes, String owners, String searchvalue,
			int level, int pageSize, int toPage) {
		// TODO Auto-generated method stub
		AutoCasesExample example = new AutoCasesExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsdeleteEqualTo(0);
		if (null != searchvalue && !"".equals(searchvalue)) {
			example.or().andNameLike("%"+searchvalue+"%");
			example.or().andClazzLike("%"+searchvalue+"%");
			example.or().andScensLike("%"+searchvalue+"%");
			example.or().andOwnerLike("%"+searchvalue+"%");
		} else {
			if (null != name && !"".equals(name)) {
				criteria.andNameEqualTo(name);
			}
			if (null != classes && !"".equals(classes)) {
				criteria.andClazzEqualTo(classes);
			}
			if (null != scenes && !"".equals(scenes)) {
				criteria.andScensEqualTo(scenes);
			}
			if (null != owners && !"".equals(owners)) {
				criteria.andOwnerEqualTo(owners);
			}
			if (level > 0) {
				criteria.andLevelEqualTo(level);
			}
		}
		// 分页处理
		PageHelper.startPage(toPage, pageSize);
		List<AutoCases> list = mapper.selectByExample(example);
		// 创建一个返回值对象
		PaginationResult result = new PaginationResult();
		result.setRows(list);
		PageInfo<AutoCases> pageInfo = new PageInfo<AutoCases>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public int updateCase(long id, String name, String clazz, String scenes, String owner, String remark, int level, String url) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		AutoCasesExample example = new AutoCasesExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		// TODO Auto-generated method stub
		List<AutoCases> list = mapper.selectByExample(example);
		// 判断是否查询到结果
		if (list != null && list.size() > 0) {
			AutoCases each = list.get(0);
			each.setName(name);
			each.setClazz(clazz);
			each.setScens(scenes);
			each.setOwner(owner);
			each.setRemark(remark);
			each.setLevel(level);
			each.setUrl(url);
			int result = mapper.updateByPrimaryKey(each);
			return result;
		} else {
			return 0;
		}
	}

	@Override
	public int deleteCase(List<Long> ids) {
		// TODO Auto-generated method stub
		int result = mapper.batchDelete(ids);
	    return result;
	}
	
	@Override
	public int softDeleteCase(List<Long> ids) {
		// TODO Auto-generated method stub
		int result = mapper.batchUpdate(ids);
	    return result;
	}

	@Override
	public PaginationResult searchCaseData(int caseid,int level, String searchvalue, int pageSize,
			int toPage) {
		// TODO Auto-generated method stub
		AutoDatasInfExample example = new AutoDatasInfExample();
		if (null != searchvalue && !"".equals(searchvalue)) {
			example.or().andNameLike("%"+searchvalue+"%");
			//加一列text查询
			example.or().andScensLike("%"+searchvalue+"%");
			example.or().andOwnersLike("%"+searchvalue+"%");
		} else {
			com.smarttest.portal.pojo.AutoDatasInfExample.Criteria criteria = example.createCriteria();
			if (caseid>-1) {
				criteria.andCaseidEqualTo(caseid);
			}
			if (level>0) {
				criteria.andLevelEqualTo(level);
			}
		}
		// 分页处理
		PageHelper.startPage(toPage, pageSize);
		List<AutoDatasInf> list = mapperInf.selectByExample(example);
		// 创建一个返回值对象
		PaginationResult result = new PaginationResult();
		result.setRows(list);
		PageInfo<AutoDatasInf> pageInfo = new PageInfo<AutoDatasInf>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	@Override
	public Map uploadCase(MultipartFile uploadFile) {
		Map resultMap = new HashMap<>();
		try {
			//取原始文件名
			String oldName = uploadFile.getOriginalFilename();
			//生成新文件名
			//用例上传
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, 
					FTP_BASE_PATH, oldName, uploadFile.getInputStream());
			//返回结果
			if(!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", FTP_BASE_PATH + oldName);
			return resultMap;
		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}

	@Override
	public SFTPOPBean downLoadSingleCase(String url) {
		// TODO Auto-generated method stub
		SFTPOPBean result = FtpUtil.downloadSingleFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, url);
		return result;
	}
}
